package DesignPattern.prototype;

/**
 * @Author cj
 * @Date 2024/8/10 20:31
 */
public class Prototype implements Cloneable {
    private int value;

    public Prototype(int value) {
        this.value = value;
    }

    // 覆盖clone方法
    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public static void main(String[] args) {
        try {
            Prototype original = new Prototype(123);
            Prototype copy = (Prototype) original.clone();

            // 修改复制对象的值，不会影响原始对象
            copy.setValue(456);

            System.out.println(original.getValue());
            System.out.println(copy.getValue());
            System.out.println(original.getValue());
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }
}
